<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 促销操作类
 * @package  	htdocs
 * @subpackage  module
 * @create by allen.qiang
 * @version 	1.0
 */
class pointsmallHandler {

    private $dbca;
	
	public function __construct( $dbca=null ) {
		if (!is_null($dbca))
			$this->dbca = $dbca;
	}
	
	/**
	 * 初始化积分商城
	 * @param $arg_array 参数数组
	 */
	public function pointsmallInit( $arg_array ){
		
		//sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
		
        //促销时间
        if (isset($arg_array['datetime']) && !empty($arg_array['datetime'])) {
            $sql .= ' AND startDate <= ? AND endDate >= ?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['datetime'];
            $params_array[] = $arg_array['datetime'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }
		
        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM promotion_pointsmall ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
	        $search_sql = 'SELECT * FROM promotion_pointsmall ' . $sql;
	        $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
	        if (!$temp_item_array)
	            return null;
	
	        foreach($temp_item_array as &$item) {
	            $item_array[] = array(
	                'promotionID' => $item->promotionID,
	                'promotionName' => $item->promotionName,
	                'promotionIndex' => $item->promotionIndex,
	                'startDate' => $item->startDate,
	                'endDate' => $item->endDate,
	                'type' => $item->type,
	                'remark' => $item->remark,
	                'useFlg' => $item->useFlg
	            );
	        }

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
        
        if ( !$item_array ) {
        	return null;
        }
        
        foreach ($item_array as &$item) {
        	
        	if ($item['type'] == 1) { //积分换券
        		
        		//组织参数
        		$arg_coupon_array = array(
        			'promotionID' => $item['promotionID'],
        			'rp' => 5
        		);
        		
        		//查询优惠券信息
        		$item['coupon'] = $this->pointsmallCoupon( $arg_coupon_array );
        		$item['count'] = $arg_coupon_array['count'];
        		
        	} else if ( in_array($item['type'],array(2,3))) { //积分兑换、积分换购
        		
        		//组织参数
        		$arg_goods_array = array(
        			'promotionID' => $item['promotionID'],
        			'rp' => 5
        		);
        		
        		//查询商品信息
        		$item['goods'] = $this->pointsmallGoods( $arg_goods_array );
        		$item['count'] = $arg_goods_array['count'];
        	}
        }
        
        return $item_array;
		
	}
	
	/**
	 * 获取积分商城促销
	 * @param $arg_array 参数数组
	 */
	public function pointsmall( $arg_array ){
		
		//sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        
        //促销ID
        if (isset($arg_array['promotionID']) && is_numeric($arg_array['promotionID'])) {
        	$sql .= ' AND promotionID = ?';
        	$params_type .= 'i';
        	$params_array[] = $arg_array['promotionID'];
        }
		
        //促销时间
        if (isset($arg_array['datetime']) && !empty($arg_array['datetime'])) {
            $sql .= ' AND startDate <= ? AND endDate >= ?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['datetime'];
            $params_array[] = $arg_array['datetime'];
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }
		
        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM promotion_pointsmall ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
	        $search_sql = 'SELECT * FROM promotion_pointsmall ' . $sql;
	        $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
	        if (!$temp_item_array)
	            return null;
	
	        foreach($temp_item_array as &$item) {
	            $item_array[] = array(
	                'promotionID' => $item->promotionID,
	                'promotionName' => $item->promotionName,
	                'promotionIndex' => $item->promotionIndex,
	                'startDate' => $item->startDate,
	                'endDate' => $item->endDate,
	                'type' => $item->type,
	                'remark' => $item->remark,
	                'useFlg' => $item->useFlg
	            );
	        }
	        
	        return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
        
	}
	
	/**
	 * 查询积分优惠券
	 * @param $arg_array 参数数组
	 */
	public function pointsmallCoupon( &$arg_array ){
		
		//sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //促销ID
        if (isset($arg_array['promotionID']) && is_numeric($arg_array['promotionID'])) {
            $sql .= ' AND pp.promotionID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['promotionID'];
        }
        
        //促销时间
        if (isset($arg_array['datetime']) && is_numeric($arg_array['datetime'])) {
            $sql .= ' AND pp.startDate <= ? AND pp.endDate >= ?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['datetime'];
            $params_array[] = $arg_array['datetime'];
        }
        
        //限制为查询积分兑换优惠券
        $sql .= ' AND pp.type = 1';
		
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM promotion_pointsmall pp
                				JOIN promotion_pointsmall_detail ppd ON pp.promotionID = ppd.promotionID
                				JOIN promotion_coupon pc ON ppd.id = pc.couponID ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT ppd.*,pc.* FROM promotion_pointsmall pp
            					JOIN promotion_pointsmall_detail ppd ON pp.promotionID = ppd.promotionID
                				JOIN promotion_coupon pc ON ppd.id = pc.couponID ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;
			
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'promotionID'  => $item->promotionID,
                    'id'  => $item->id,
                    'name' => $item->name,
                    'points'  => $item->points,
                    'pointsPrice'  => $item->pointsPrice,
                    'couponName' => $item->couponName,
                    'img_link' => G_THEME_IMAGE_URL . '/comm/lppz.jpg' //默认优惠券图片
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}
	
	/**
	 * 查询商品
	 * @param $arg_array 参数数组
	 */
	public function pointsmallGoods( &$arg_array ){
		
		//sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //促销ID
        if (isset($arg_array['promotionID']) && is_numeric($arg_array['promotionID'])) {
            $sql .= ' AND pp.promotionID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['promotionID'];
        }
        
        //促销时间
        if (isset($arg_array['datetime']) && is_numeric($arg_array['datetime'])) {
            $sql .= ' AND pp.startDate <= ? AND pp.endDate >= ?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['datetime'];
            $params_array[] = $arg_array['datetime'];
        }
        
        //限制为查询积分兑换或积分换购
        $sql .= ' AND pp.type IN (2,3)';
		
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM promotion_pointsmall pp
                				JOIN promotion_pointsmall_detail ppd ON pp.promotionID = ppd.promotionID
                				JOIN goods_sale gs ON ppd.id = gs.product_code ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT ppd.*,gs.* FROM promotion_pointsmall pp
            					JOIN promotion_pointsmall_detail ppd ON pp.promotionID = ppd.promotionID
                				JOIN goods_sale gs ON ppd.id = gs.product_code ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;
			
            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'promotionID'  => $item->promotionID,
                    'id'  => $item->id,
                    'name' => $item->name,
                    'points'  => $item->points,
                    'pointsPrice'  => $item->pointsPrice,
                    'salePrice'  => $item->salePrice,
                    'link' => G_WEBSITE_DOMAINNAME . '/shop/goods-' . $item->postID . '.html',
                    'wap_link' => G_WEBSITE_DOMAINNAME . '/wap/goods-' . $item->postID . '.html',
                    'img_link' => G_IMAGE_URL . '/goods/' . $item->goodsMainPic,
                    'goodsName' => $item->goodsName
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	}
	
}